<?php
/**
 * $Id: viewcat.php v 1.05 06 july 2004 Liquid Exp $
 * Module: WF-Downloads
 * Version: v2.0.5a
 * Release Date: 26 july 2004
 * Author: WF-Sections
 * Licence: GNU
 */
 
include 'header.php';
include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';

global $xoopsModuleConfig, $myts, $xoopsModules;

$start = isset($_GET['start']) ? intval($_GET['start']) : 0;
$orderby = isset($_GET['orderby']) ? convertorderbyin($_GET['orderby']) : $xoopsModuleConfig['filexorder'];
$cid = (isset($_GET['cid']) && $_GET['cid'] > 0) ? $_GET['cid'] : 0;

$xoopsOption['template_main'] = 'wfdownloads_viewcat.html';
$groups = (is_object($xoopsUser)) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
$gperm_handler = &xoops_gethandler('groupperm');

/**
 * Begin Main page Heading etc
 */
include XOOPS_ROOT_PATH . '/header.php';

$catarray['imageheader'] = wfd_imageheader();
$catarray['letters'] = wfd_letters();
$catarray['toolbar'] = wfd_toolbar();
$xoopsTpl->assign('catarray', $catarray);

/**
 * Breadcrumb
 */
$mytree = new XoopsTree($xoopsDB->prefix('wfdownloads_cat'), "cid", "pid");
$pathstring = "<a href='index.php'>" . _MD_WFD_MAIN . "</a>&nbsp;:&nbsp;";
$pathstring .= $mytree->getNicePathFromId($cid, "title", "viewcat.php?op=");
$xoopsTpl->assign('category_path', $pathstring);
$xoopsTpl->assign('category_id', $cid);

$arr = $mytree->getFirstChild($cid, "weight");

/**
 * Display Sub-categories for selected Category
 */
if (is_array($arr) > 0 && !isset($_GET['list']) && !isset($_GET['selectdate']))
{
    $scount = 1;
    foreach($arr as $ele)
    {
        if (!$gperm_handler->checkRight('WFDownCatPerm', $ele['cid'], $groups, $xoopsModule->getVar('mid')))
        {
            continue;
        } 

        $sub_arr = array();
        $sub_arr = $mytree->getFirstChild($ele['cid'], "weight");
        $space = 0;
        $chcount = 0;
        $infercategories = "";

        foreach($sub_arr as $sub_ele)
        {
            /**
             * Subitem file count
             */
            $hassubitems = wfd_getTotalItems($sub_ele['cid']);
            /**
             * Filter group permissions
             */
            if ($gperm_handler->checkRight('WFDownCatPerm', $sub_ele['cid'], $groups, $xoopsModule->getVar('mid')))
            {
                /**
                 * If subcategory count > 5 then finish adding subcats to $infercategories and end
                 */
                if ($chcount > 5)
                {
                    $infercategories .= "...";
                    break;
                } 
                if ($space > 0) $infercategories .= ", ";
                $infercategories .= "<a href='" . XOOPS_URL . "/modules/wfdownloads/viewcat.php?cid=" . $sub_ele['cid'] . "'>" . $myts->htmlSpecialChars($sub_ele['title']) . "</a> (" . $hassubitems['count'] . ")";
                $space++;
                $chcount++;
            } 
        } 
        $totallinks = wfd_getTotalItems($ele['cid']);
        $xoopsTpl->append('subcategories', array('title' => $myts->htmlSpecialChars($ele['title']),
                'id' => $ele['cid'], 'infercategories' => $infercategories, 'totallinks' => $totallinks['count'],
                'count' => $scount));
        $scount++;
    } 
} 

/**
 * Show Description for Category listing
 */
$sql = "SELECT description, nohtml, nosmiley, noxcodes, noimages, nobreak FROM " . $xoopsDB->prefix('wfdownloads_cat') . " WHERE cid = $cid";
$head_arr = $xoopsDB->fetchArray($xoopsDB->query($sql));
$html = ($head_arr['nohtml']) ? 0 : 1;
$smiley = ($head_arr['nosmiley']) ? 0 : 1;
$xcodes = ($head_arr['noxcodes']) ? 0 : 1;
$images = ($head_arr['noimages']) ? 0 : 1;
$breaks = ($head_arr['nobreak']) ? 1 : 0;
$description = $myts->displayTarea($head_arr['description'], $html, $smiley, $xcodes, $images, $breaks);
$xoopsTpl->assign('description', $description);

/**
 * Extract Download information from database
 */
$xoopsTpl->assign('show_categort_title', true);
$sql = "SELECT * FROM " . $xoopsDB->prefix('wfdownloads_downloads') . " ";
if (isset($_GET['selectdate']))
{
    $sql .= "WHERE TO_DAYS(FROM_UNIXTIME(published)) = TO_DAYS(FROM_UNIXTIME(" . $_GET['selectdate'] . ")) 
			AND published > 0 AND published <= " . time() . " AND (expired = 0 OR expired > " . time() . ") 
			AND offline = 0 ORDER BY published DESC";
    $result = $xoopsDB->query($sql, $xoopsModuleConfig['perpage'] , $start);
    $total_numrows['count'] = $xoopsDB->getRowsNum($xoopsDB->query($sql));
} elseif (isset($_GET['list']))
{
    $sql .= "WHERE title LIKE '" . $_GET['list'] . "%' AND published > 0 AND 
			published <= " . time() . " AND (expired = 0 OR expired > " . time() . ") AND offline = 0 
			ORDER BY " . $orderby;
    $result = $xoopsDB->query($sql, $xoopsModuleConfig['perpage'] , $start);
    $total_numrows = wfd_getTotalItems($cid);
} 
else
{
    $sql .= "WHERE cid=" . $cid . " AND published > 0 AND published <= " . time() . " 
			AND (expired = 0 OR expired > " . time() . ") AND offline = 0 
			ORDER BY " . $orderby;
    $result = $xoopsDB->query($sql, $xoopsModuleConfig['perpage'] , $start);
    $xoopsTpl->assign('show_categort_title', false);
    $total_numrows = wfd_getTotalItems($cid);
} 
/**
 * Show Downloads by file
 */
if ($total_numrows['count'] > 0)
{
    while ($down_arr = $xoopsDB->fetchArray($result))
    {
        if ($gperm_handler->checkRight('WFDownFilePerm', $down_arr['lid'], $groups, $xoopsModule->getVar('mid')))
        {
            include XOOPS_ROOT_PATH . '/modules/wfdownloads/include/downloadinfo.php';
        } 
    } 

    /**
     * Show order box
     */
    $xoopsTpl->assign('show_links', false);
    if ($total_numrows['count'] > 1 && $cid != 0)
    {
        $xoopsTpl->assign('show_links', true);
        $orderbyTrans = convertorderbytrans($orderby);
        $xoopsTpl->assign('lang_cursortedby', sprintf(_MD_WFD_CURSORTBY, convertorderbytrans($orderby)));
        $orderby = convertorderbyout($orderby);
    } 
    /**
     * Screenshots display
     */
    $xoopsTpl->assign('show_screenshot', false);
    if (isset($xoopsModuleConfig['screenshot']) && $xoopsModuleConfig['screenshot'] == 1)
    {
        $xoopsTpl->assign('shots_dir', $xoopsModuleConfig['screenshots']);
        $xoopsTpl->assign('shotwidth', $xoopsModuleConfig['shotwidth']);
        $xoopsTpl->assign('shotheight', $xoopsModuleConfig['shotheight']);
        $xoopsTpl->assign('show_screenshot', true);
    } 

    /**
     * Nav page render
     */
    include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
    if (isset($_GET['selectdate']))
    {
        $pagenav = new XoopsPageNav($total_numrows['count'], $xoopsModuleConfig['perpage'] , $start, 'start', 'list=' . $_GET['selectdate']);
    } 
    else
    {
        $pagenav = new XoopsPageNav($total_numrows['count'], $xoopsModuleConfig['perpage'] , $start, 'start', 'cid=' . $cid);
    } 
    $page_nav = $pagenav->renderNav();
    $istrue = (isset($page_nav) && !empty($page_nav)) ? true : false;
    $xoopsTpl->assign('page_nav', $istrue);
    $xoopsTpl->assign('pagenav', $page_nav);
} 
include XOOPS_ROOT_PATH . '/footer.php';

?>
